package org.java.mathgame.screens;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import org.java.mathgame.MathGame;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;


public class HowToPlayScreen implements Screen {
    private MathGame game;
    private SpriteBatch spriteBatch;
    private BitmapFont font;
    private Stage stage;
    // constructor to keep a reference to the main Game class
    public HowToPlayScreen(final MathGame game){
        this.game = game;
        spriteBatch = new SpriteBatch();
        stage = new Stage();
        font = new BitmapFont();
        TextButton.TextButtonStyle infoStyle = new TextButton.TextButtonStyle();
        infoStyle.up = new ButtonBackground(new Color(35/255f,35/255f,35/255f,1));
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        infoStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        Label.LabelStyle backStyle = new Label.LabelStyle();
        backStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        backStyle.background = new ButtonBackground(new Color(0,0,0,0));

        Label backLabel = new Label("  <",backStyle);
        TextButton backButton = new TextButton("How to play ",infoStyle);

        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        labelStyle.fontColor = Color.BLACK;
        backButton.getLabel().remove();
        backButton.clear();
        backButton.add(backLabel).expandX().left();
        backButton.add(backButton.getLabel());
        backButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen(game.mainMenuScreen);
            }
        });
        Texture infoTexture = new Texture("data/info.png");
        infoTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
        ImageButton infoButton = new ImageButton(new SpriteDrawable(new Sprite(infoTexture)));
        Table rootTable = new Table();

        rootTable.setFillParent(true);
        rootTable.setBackground(new ButtonBackground(new Color(238 / 255f, 238 / 255f, 238 / 255f, 1)));
        rootTable.add(backButton).expandX().fillX().top().expandY().top();
        rootTable.row();
        Label label1 = new Label("Combine numbers and operators to achieve the goal number",labelStyle);
        label1.setWrap(true);
        rootTable.add(label1).expandX().fillX().expandY().top().padLeft(10);
        rootTable.row();
        rootTable.add(infoButton).expandX().fillX();
        rootTable.row();
        Label label2 = new Label("In Time Challenge mode, collect as many points as possible in a minute",labelStyle);
        label2.setWrap(true);
        rootTable.add(label2).expandX().fillX().expandY().top().padLeft(10);
        rootTable.row();
        Label label3 = new Label("In Field Clearing mode, quickly remove all the numbers from the grid",labelStyle);
        label3.setWrap(true);
        rootTable.add(label3).expandX().fillX().expandY().top().padLeft(10);
        rootTable.pad(10);
        stage.addActor(rootTable);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.5f, 0.3f, 0.6f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        font.setColor(1f, 1f, 1f, 1f);
        spriteBatch.begin();
        //font.draw(spriteBatch, "howToPlayScreen", Gdx.graphics.getHeight()/2, Gdx.graphics.getWidth()/2);
        spriteBatch.end();
        stage.draw();
        if (Gdx.input.justTouched()) // use your own criterion here
            game.setScreen(game.mainMenuScreen);
    }


    @Override
    public void resize(int width, int height) {
    }


    @Override
    public void show() {
        // called when this screen is set as the screen with game.setScreen();
    }


    @Override
    public void hide() {
        // called when current screen changes from this to a different screen
    }


    @Override
    public void pause() {
    }


    @Override
    public void resume() {
    }


    @Override
    public void dispose() {
        // never called automatically
    }
}